home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / animation / menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.6 KB  |  205 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  menu.c - use popup menus to control animation of a robot arm
  19.  *
  20.  *  Right Mousebutton    - popup menu
  21.  *  Escape key          - exit the program 
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26. #include <math.h>    /* for fmodf */
  27. #include <stdlib.h>    /* for NULL */
  28. #include <stdio.h>    /* for printf */
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid  animate( GLvoid );
  34. GLvoid  visibility( GLint );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  keyboard( GLubyte, GLint, GLint );
  38. GLvoid  menu( GLint );
  39.  
  40. void printHelp( char * );
  41.  
  42. /*  Global Variables */
  43.  
  44. static GLfloat    shoulderAngle = 0.0;    /* controls shoulder rotation */
  45.  
  46. static GLboolean spinning = GL_TRUE;
  47.  
  48. /* Global Definitions */
  49.  
  50. #define KEY_ESC    27    /* ascii value for the escape key */
  51.  
  52. #define MENU_SPIN    1    /* animation control option */
  53. #define MENU_EXIT    2    /* exit option */
  54.  
  55. void
  56. main( int argc, char *argv[] )
  57. {
  58.     GLsizei width, height;
  59.  
  60.     glutInit( &argc, argv );
  61.  
  62.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  63.     height = glutGet( GLUT_SCREEN_HEIGHT );
  64.     glutInitWindowPosition( 0, height / 4 );
  65.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  66.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  67.     glutCreateWindow( argv[0] );
  68.  
  69.     initgfx();
  70.  
  71.     glutIdleFunc( animate );
  72.     glutVisibilityFunc( visibility );
  73.     glutKeyboardFunc( keyboard );
  74.     glutReshapeFunc( reshape );
  75.     glutDisplayFunc( drawScene ); 
  76.  
  77.  
  78.     /* create menu and add menu options */
  79.     glutCreateMenu( menu );
  80.     glutAddMenuEntry( "Stop Rotating Arm", MENU_SPIN );
  81.     glutAddMenuEntry( "Exit", MENU_EXIT );
  82.     glutAttachMenu( GLUT_RIGHT_BUTTON );
  83.  
  84.     printHelp( argv[0] );
  85.  
  86.     glutMainLoop();
  87. }
  88.  
  89. void
  90. printHelp( char *progname )
  91. {
  92.     fprintf(stdout, "\n%s - smoothly animate a robot arm\n\n"
  93.         "Axes: X - red, Y - green, Z - blue\n\n"
  94.         "Right Mousebutton    - popup menu\n"
  95.         "Escape Key        - exit the program\n",
  96.         progname);
  97. }
  98.  
  99. GLvoid
  100. initgfx( GLvoid )
  101. {
  102.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  103.     glShadeModel( GL_FLAT );
  104.     glEnable( GL_DEPTH_TEST );
  105. }
  106.  
  107. GLvoid
  108. reshape( GLsizei width, GLsizei height )
  109. {
  110.     GLdouble    aspect;
  111.  
  112.     glViewport( 0, 0, width, height );
  113.  
  114.     aspect = (GLdouble) width / (GLdouble) height;
  115.  
  116.     glMatrixMode( GL_PROJECTION );
  117.     glLoadIdentity();
  118.     gluPerspective( 45.0, aspect, 1.0, 20.0 );
  119.     glMatrixMode( GL_MODELVIEW );
  120. }
  121.  
  122. GLvoid 
  123. keyboard( GLubyte key, GLint x, GLint y )
  124. {
  125.     switch (key) {
  126.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  127.         exit(0);
  128.     }
  129. }
  130.  
  131. GLvoid 
  132. animate( GLvoid )
  133. {
  134.     /* update the rotation of the shoulder for each scene */
  135.     shoulderAngle = fmodf( (shoulderAngle + 1.0), 360.0 );
  136.  
  137.     glutPostRedisplay();    /* Tell GLUT to redraw the scene */
  138. }
  139.  
  140. GLvoid
  141. visibility( int state ) 
  142. {
  143.     /* restart the animation function if we were
  144.      * animated when the window was hidden
  145.      */
  146.     if (state == GLUT_VISIBLE && spinning) {
  147.         glutIdleFunc( animate );
  148.     } else {
  149.         glutIdleFunc( NULL );
  150.     }
  151. }
  152.  
  153. GLvoid
  154. menu( int value )
  155. {
  156.  
  157.     switch (value) {
  158.     case MENU_SPIN:
  159.         spinning = !spinning;
  160.         if (spinning) {
  161.             glutIdleFunc( animate );
  162.             glutChangeToMenuEntry( MENU_SPIN, "Stop Rotating Arm",
  163.                         MENU_SPIN );
  164.         } else {
  165.             glutIdleFunc( NULL );
  166.             glutChangeToMenuEntry( MENU_SPIN, "Start Rotating Arm",
  167.                         MENU_SPIN );
  168.         }
  169.         break;
  170.     case MENU_EXIT:
  171.         exit(0);
  172.     }
  173. }
  174.  
  175. GLvoid
  176. drawScene( GLvoid )
  177. {
  178.     static GLfloat    upperArmColor[] = { 1.0, 0.0, 0.0 };
  179.     static GLfloat    lowerArmColor[] = { 0.8, 0.5, 0.5 };
  180.  
  181.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  182.  
  183.     glPushMatrix();
  184.         gluLookAt( 0.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
  185.         XYaxes();
  186.  
  187.         /* rotate the shoulder by shoulderAngle degrees */
  188.         glRotatef( shoulderAngle, 0.0, 0.0, 1.0 );
  189.  
  190.         /* Draw the upper arm */
  191.         glTranslatef( 1.0, 0.0, 0.0 ); 
  192.             glColor3fv( upperArmColor );
  193.         WireBox( 2.0, 0.4, 1.0 );
  194.  
  195.         /* Draw the lower arm */
  196.         glTranslatef( 1.0, 0.0, 0.0 );
  197.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  198.         glTranslatef( 1.0, 0.0, 0.0 );
  199.             glColor3fv( lowerArmColor );
  200.         WireBox( 2.0, 0.4, 1.0 );
  201.     glPopMatrix();
  202.  
  203.     glutSwapBuffers();
  204. }
  205.